import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testnamespace:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/namespace/1_get_namespace_list.yaml'))
    def test_get_namespace_list(self, namespaceinfo):
        # 获取namespace列表接口-正常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url']
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/namespace/2_create_namespace.yaml'))
    def test_create_namespace(self, namespaceinfo):
        # 创建namespace接口-正常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        data = namespaceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/3_get_specific_namespace.yaml'))
    def test_get_specific_namespace(self, namespaceinfo):
        # 获取指定namespace接口-正常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/namespace/4_update_namespace.yaml'))
    def test_update_namespace(self, namespaceinfo):
        # 更新指定namespace接口-正常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        namespaceinfo['request']['data'] = globals()['updatereqbody']
        data = namespaceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/5_update_section_namespace.yaml'))
    def test_update_section_namespace(self, namespaceinfo):
        # 部分更新指定namespace的接口-正常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        data = namespaceinfo['request']['data']
        header = namespaceinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/6_delete_specific_namespace.yaml'))
    def test_delete_specific_namespace(self, namespaceinfo):
        # 删除namespace接口-正常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/1_get_namespace_list_exception.yaml'))
    def test_get_namespace_list_exception(self, namespaceinfo):
        # 获取namespace列表接口-异常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url']
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/2_create_namespace_exception.yaml'))
    def test_create_namespace_exception(self, namespaceinfo):
        # 创建namespace接口-异常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        data = namespaceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/3_get_specific_namespace_exception.yaml'))
    def test_get_specific_namespace_exception(self, namespaceinfo):
        # 获取指定namespace接口-异常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/4_update_namespace_exception.yaml'))
    def test_update_namespace_exception(self, namespaceinfo):
        # 更新指定namespace接口-异常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        namespaceinfo['request']['data'] = globals()['updatereqbody']
        data = namespaceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/5_update_section_namespace_exception.yaml'))
    def test_update_section_namespace_exception(self, namespaceinfo):
        # 部分更新指定namespace的接口-异常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        data = namespaceinfo['request']['data']
        header = namespaceinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.namespace
    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/namespace/6_delete_specific_namespace_exception.yaml'))
    def test_delete_specific_namespace_exception(self, namespaceinfo):
        # 删除namespace接口-异常
        base_url = datas.baseUrl + Testnamespace.url_unify
        url = base_url + namespaceinfo['request']['url'].replace('{namespace}', 'mynamespace')
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']
